﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace BlastExplorer {
    /// <summary>
    /// Interaction logic for SequenceLoader.xaml
    /// </summary>
    public partial class SequenceLoader : Window {
        private static string query = // Settings.DefaultGI;
            @">gi|187935881|gb|ACD37439.1| hemagglutinin [Influenza A virus (A/Hong Kong/2652/2006(H1N1))]
MKVKLLVLLCTFTATYADTICIGYHANNSTDTVDTVLEKNVTVTHSVNLLEDNHNGKLCLLKGIAPLQLG
NCSVAGWILGNPECELLISKESWSYIVEKPNPENGTCYPGHFADYEELREQLSSVSSFERFEIFPKESSW
PNHTVTGVSASCSHNGESSFYKNLLWLTGKNGLYPNLSKSYANNKEKEVLVLWGVHHPPNIGNQMTLYHK
ENAYVSVVSSHYSRKFTPEIAKRPKVRDQEGRINYYWTLLEPGDTIIFEANGNLIAPRYAFALSRGFGSG
IINSNAPMDECDAKCQTPQGAINSSLPFQNVHPVTIGECPKYVRSAKLRMVTGLRNIPSIQSRGLFGAIA
GFIEGGWTGMVDGWYGYHHQNEQGSGYAADQKSTQNAI
";

		public SequenceLoader () {
			InitializeComponent();

			queryField.Text = query;
			eValueCutoffField.Text = Settings.DefaultEValue.ToString();
			cddEValueCutoffField.Text = Settings.DefaultCddEValue.ToString();
			hitListSizeField.Text = Settings.DefaultHitListSize.ToString();
		}

		public void Initialise (
			double eValueCutoff,
			double cddEValueCutoff,
			int hitListSize,
			bool useBrowserProxy,
			Taxon[] selectedTaxa
		) {
			useBrowserProxyField.IsChecked = useBrowserProxy;
			eValueCutoffField.Text = eValueCutoff.ToString();
			cddEValueCutoffField.Text = cddEValueCutoff.ToString();
			hitListSizeField.Text = hitListSize.ToString();
		}

		private void Cancel_Click ( object sender, RoutedEventArgs e ) {
			this.DialogResult = false;
		}

		private void Load_Click ( object sender, RoutedEventArgs e ) {
			query = queryField.Text.Trim();
			this.DialogResult = true;
		}

		static public string Query {
			get {
				return query;
			}
			set {
				query = value;
			}
		}

		public bool UseBrowserProxy {
			get {
				return useBrowserProxyField.IsChecked == true;
			}
		}


		#region Property: EValueCutoff
		/// <summary>
		/// Gets the E-value cutoff.
		/// </summary>

		public double EValueCutoff {
			get {
				try {
					return double.Parse( eValueCutoffField.Text );
				}
				catch {
					return Settings.DefaultEValue;
				}
			}
		}
		#endregion

		#region Property: CddEValueCutoff
		/// <summary>
		/// Gets the Cdd E-value cutoff.
		/// </summary>

		public double CddEValueCutoff {
			get {
				try {
					return double.Parse( cddEValueCutoffField.Text );
				}
				catch {
					return Settings.DefaultCddEValue;
				}
			}
		}
		#endregion
		
		/// <summary>
		/// Gets the hit list size.
		/// </summary>
		
		public int HitListSize {
			get { 
				try {
					return int.Parse( hitListSizeField.Text );
				}
				catch {
					return Settings.DefaultHitListSize;
				}
			}
		}

        private void button1_Click(object sender, RoutedEventArgs e) {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".fasta"; // Default file extension
            dlg.Filter = "FastA documents (.fasta)|*.fasta"; // Filter files by extension

            Nullable<bool> result = dlg.ShowDialog();

            if (result == true) {
                String text = "";
                using (StreamReader reader = new StreamReader(dlg.FileName)) {
                    while (!reader.EndOfStream) {
                        text += reader.ReadLine() + "\n";
                    }
                }
                queryField.Text = text;
            }

        }
	}
}
